var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

const auth = require('./routes/auth')
const projects = require('./routes/projects')
const user = require('./routes/user')
const kanbans = require('./routes/kanbans')
const task = require('./routes/task')
const epic = require('./routes/epic')

var app = express();

// 视图引擎设置
// 告诉express框架模板文件所在的位置
app.set('views', path.join(__dirname, 'views'));
// 告诉express框架模板后缀是什么
app.set('view engine', 'jade');

// 日志
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use(auth)
app.use('/projects',projects)
app.use('/users',user)
app.use('/kanbans',kanbans)
app.use(task)
app.use('/epics',epic)

// 捕获404并转发到错误处理程序
app.use(function(req, res, next) {
  next(createError(404));
});

// 错误处理器
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});



module.exports = app;
